package com.huiminpay.merchant.exception;

import com.huiminpay.common.cache.domain.CommonErrorCode;
import com.huiminpay.common.cache.domain.ErrorCode;
import com.huiminpay.common.cache.exception.BizException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 王帅坤
 * @date 2021年06月10日12:11
 */
@ControllerAdvice
public class GlobalException {


    @ExceptionHandler
    @ResponseBody
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public  RestErrorResponse  handler(HttpServletRequest request, HttpServletResponse response,
                          Exception e){

        if (e instanceof BizException){
            //自定义异常拦截
            BizException bizException = (BizException) e;
            ErrorCode errorCode = bizException.getErrorCode();

            return new RestErrorResponse(errorCode.getCode()+"",errorCode.getDesc());


        }
//        未知的异常
        e.printStackTrace();
        return new RestErrorResponse(CommonErrorCode.UNKOWN.getCode()+"",CommonErrorCode.UNKOWN.getDesc());
    }
}
